﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Xsl;
using System.Xml.XPath;

namespace Catalogue
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("../../Catalogue.xml");

            XmlNode rootNode = doc.DocumentElement;

            Dictionary<string,int> dict = new Dictionary<string, int>();

            foreach (XmlNode node in rootNode.ChildNodes)
            {
                string albumName = node["name"].InnerText;
                string artistName = node["artist"].InnerText;

                Console.WriteLine("Album Name = {0} , Artist Name = {1}",albumName , artistName);

                if (dict.ContainsKey(artistName))
                {
                    dict[artistName]++;
                }
                else
                    dict.Add(artistName , 1);
            }

            Console.WriteLine();
            foreach (var d in dict)
            {
                Console.WriteLine(d.Key + " has " + d.Value +" albums");
            }
            Console.WriteLine();

            List<XmlNode> candidateForDelete = new List<XmlNode>();

            foreach (XmlNode node in rootNode.ChildNodes)
            {
                if (double.Parse(node["price"].InnerText) > 20.0)
                {
                    candidateForDelete.Add(node);
                }
            }
            foreach (XmlNode node in candidateForDelete)
                rootNode.RemoveChild(node);

            doc.Save("../../CatalogueNew.xml");
            

            /*
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("../../Catalogue.xml");
            string xPathQuery = "/Catalogue/album";

            XmlNodeList albumsList = xmlDoc.SelectNodes(xPathQuery);
            foreach (XmlNode albumNode in albumsList)
            {
                string artistName = albumNode.SelectSingleNode("artist").InnerText;
                Console.WriteLine(artistName);
            }
            */

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("../../Catalogue.xml");
            string xPathQuery = "/Catalogue/album/songs";

            XmlNodeList List = xmlDoc.SelectNodes(xPathQuery);
            foreach (XmlNode albumNode in List)
            {
                XmlNodeList songslist = albumNode.SelectNodes("title");
                foreach (XmlNode s in songslist)
                {
                 Console.WriteLine(s.InnerText);
                }
            }
            Console.WriteLine("---------\n");


            XDocument xDoc =  XDocument.Load("../../Catalogue.xml");

            var songsList =
                from song in xDoc.Descendants("songs")
                select song.Elements("title");//song.Element("title").Value;

            foreach (var song in songsList)
            {
                foreach (var s in song)
                {
                    Console.WriteLine(s.Value);
                }
            }


            XslCompiledTransform myTransform = new XslCompiledTransform();
            using (StreamReader streamReader = new StreamReader("../../AlbumAndAuthor.xsd"))
            {
                    XmlReader styleReader = XmlReader.Create(streamReader);
                    myTransform.Load(styleReader);
                    myTransform.Transform("../../Catalogue.xml", "../../Catalogue.html");
            }


        }
    }
}
